-- UIArtifactChoose
-- Create by zouyb
-- 神器选择界面

require "game/ui/form/artifact/UIArtifactRepair"
require "game/ui/form/artifact/UIArtifactCompound"

-- UIArtifactChoose继承自Layer
UIArtifactChoose = class("UIArtifactChoose", function()
    return cc.Layer:create();
end);

function UIArtifactChoose.create()
    return UIArtifactChoose.new();
end

-- 最大行数
local MIN_GRID_COLUMN = 4;
local MAX_GRID_ROW    = 2;
local ITEM_NAME       = "artifact";
-- 内部函数声明

-- 构造函数
function UIArtifactChoose:ctor(id)
    -- 初始化
    self:setName("UIArtifactChoose");
    local node = cc.CSLoader:createNode("layout/lictor/LictorChoose.csb");
    self:addChild(node);
    self.node = node;

    local CT = node:getChildByName("CT");
    self.CT = CT;
    local scroll_view = findChildByName(self.node, "CT/bg_1/scroll_view");
    self.scrollView = scroll_view;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.selectIndex = 1;

    local explainLabel = findChildByName(self.CT, "bg_2/explain_scroll/explain1");
    self.pos = explainLabel:getPositionY();

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    self:initData();

    self:initGrids();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIArtifactChoose:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

local function sortItem(item1, item2)
    local id1 = item1.classId;
    local id2 = item2.classId;
    if ItemM.getAmount(ME.user, id1) > 0 and not(ItemM.getAmount(ME.user, id2) > 0) then
        return true;
    elseif not(ItemM.getAmount(ME.user, id1) > 0) and ItemM.getAmount(ME.user, id2) > 0 then
        return false;
    end


    local rank1 = EquipM.query(id1, "rank");
    local rank2 = EquipM.query(id2, "rank");

    if rank1 > rank2 then
        return true;
    elseif rank1 < rank2 then
        return false;
    end

    local type1 = EquipM.query(id1, "type");
    local type2 = EquipM.query(id2, "type");

    if type1 > type2 then
        return true;
    elseif type1 < type2 then
        return false;
    end

    return id1 > id2;
end

function UIArtifactChoose:initData()
    self.data = {};
    local afIds = ItemM.getArtifact();
    local afsIds = ItemM.getArtifactScrap();

    local configAfs = self:getArtifactList();
    local info = {};

    for _, classId in pairs(configAfs) do
        info = ItemM.query(classId);
        local item = {};
        item.id = info.class_id;
        item.name = info.name;
        item.rank = info.rank;
        item.desc = ItemM.getItemDesc(classId);
        item.durability = ArtifactM.getDurability(classId) or 0; -- 剩余耐久度
        item.durabilityAll = ArtifactM.query(classId, "max_durability") or 0; -- 总耐久度
        local material = ArtifactM.getArtifactScrap(classId);
        if not material then
            error("获取不到合成材料");
        end
        item.fragmentCount = ItemM.getAmount(ME.user, material[1]); -- 碎片数
        item.fragmentNeedCount = material[2]; -- 合成需求碎片数
        item.classId = classId;
        item.isHas = true; -- 是否已经拥有

        if table.indexOf(afIds, classId) == -1 then
            item.isHas = false;
        end

        table.insert(self.data,item);
    end

    table.sort(self.data, sortItem);
end

-- 初始化格子
function UIArtifactChoose:initGrids(node)
    local w = DESIGN_WIDTH;
    local h = DESIGN_HEIGHT;

    -- 图标间距(横向)
    local wGap = 7;

    -- 图标间距(纵向)
    local hGap = 18;

    -- 图标宽度
    local iconWidth = 110;

    -- 图片高度
    local iconHeight = 115;

    local topHGap  = 3;
    local leftWGap = 24;
    local bottomHGap = 50;

    -- 神器列表
    local artifactList = self.data;

    -- 神器个数
    local artifactCount = #artifactList;

    self.scrollView.rows = {};
    self.scrollView:removeAllChildren();

    local itemNode = cc.CSLoader:createNode("layout/artifactt/ArtifactChooseItem.csb");
    itemNode:setScale(0.88);
    -- 初始化滚动容器内的单元格（仅针对竖向滚动容器）
    -- @param windowNode    界面节点
    -- @param scrollNode    容器节点
    -- @param itemNode      单元格的节点
    -- @param totalNum      单元格总数
    -- @param pageRowNum    单页可以显示下的行数
    -- @param maxColNum     最大列数
    -- @param wGap          单元格间距(横向) 这里作为最小值使用
    -- @param hGap          单元格间距(纵向)
    -- @param iconWidth     单元格宽度
    -- @param iconHeight    单元格高度
    -- @param topHGap       上方留白
    -- @param bottomHGap    下方留白
    -- @param leftWGap      左右留白  这里作为最小值使用
    -- @param touchEvent    点击事件注册函数
    initScrollGridsVertical(self, self.scrollView, itemNode, artifactCount, MAX_GRID_ROW, MIN_GRID_COLUMN, wGap, hGap, iconWidth, iconHeight, topHGap, bottomHGap, leftWGap, self.registerArtifactItemTouchEvent);
end

-- 获取神器列表
function UIArtifactChoose:getArtifactList()
    local artifactList = ItemM.getArtifact();
    local artifactScrapList = ItemM.getArtifactScrap();

    for _, scrapId in pairs(artifactScrapList) do
        local artifactId = scrapId - 200;
        local canCompound = ArtifactM.canCompound(artifactId) == true;
        if table.indexOf(artifactList, artifactId) == -1 and canCompound then
            table.insert(artifactList, artifactId);
        end
    end
    return artifactList;
end

-- 显示神器选择界面
function UIArtifactChoose:redraw()
    -- 显示标题
    local titleLabel = findChildByName(self.CT, "title/title_label");
    TextStyleM.setTitleStyle(titleLabel);
    titleLabel:setString(getLocStr("artifact_choose"));

    -- 绘制副标题
    local titleExplainLabel = findChildByName(self.CT, "title/title_explain");
    titleExplainLabel:setString(getLocStr("durability_consume"));
    TextStyleM.setSubheadStyle(titleExplainLabel);

    -- 隐藏阵营相关信息
    local campIcon = findChildByName(self.CT, "bg_2/camp_icon");
    campIcon:setVisible(false);

    local campLabel = findChildByName(self.CT, "bg_2/camp_label");
    campLabel:setVisible(false);

--    local workShopBg = findChildByName(self.CT, "bg_2/work_shop_bg");
--    workShopBg:setVisible(true);

    -- 收集信息
    local totalGridCount = #self.data;

    -- 显示信息
    for i = 1, totalGridCount do
        local grid = self.scrollView:getChildByName("item" .. i);
        if grid then
            --  初始化格子信息
            self:initGridInfo(grid, self.data[i]);
        end
    end

    -- 初始化神器说明
   self:artifacttSelected(1, 1);
end

--  初始化格子信息
function UIArtifactChoose:initGridInfo(grid, data)
    -- 图标
    local focusLight = findChildByName(grid, "focus");
    focusLight:setVisible(false);
    local iconImg = findChildByName(grid, "icon");
    local fileName = ItemM.query(data.classId, "icon");
    iconImg:loadTexture(getItemIconPath(fileName));

    -- 神器等级
    local level = ArtifactM.getArtifactLevel(data.classId);

    -- 右上角等级标识
    local starNode = findChildByName(grid, "star");
    starNode:removeAllChildren();
    local levelLabel = findChildByName(grid, "level_label");
    if level >= 0 then
        local durability = ArtifactM.getDurability(data.classId) or 0; -- 剩余耐久度
        local durabilityAll = ArtifactM.query(data.classId, "max_durability") or 0; -- 总耐久度
        local lightStarPath = "images/ui/symbol/point_01.png";
        local darkStarPath = "images/ui/symbol/point_02.png";
        local sinkerLayer = createStarLayerEx(durability, durabilityAll, lightStarPath, darkStarPath, "center", -0.3);
        starNode:addChild(sinkerLayer);
    else
        levelLabel:setVisible(false);
    end

    -- 耐久度
    local stateLabel = findChildByName(grid, "state_label")
    local durability = data.durability; -- 剩余耐久度
    local count = ItemM.getAmount(ME.user, data.classId);

    -- 背景图上设置当前神器ID
    local bg = findChildByName(grid, "bg");
    bg.classId = data.classId;

    -- 根据资源获取资源背景框
    -- local bgPath = getFrameBgIconPath("rank"..data.rank);
    -- bg:loadTexture(bgPath)

    iconImg:stopAllActions();
    if data.isHas then
        levelLabel:setVisible(true);
        bg:setOpacity(255);
        if level > 0 then
            TextStyleM.setTextStyle(levelLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
            levelLabel:setString("+"..tonumber(level));

        else
            levelLabel:setVisible(false);
        end
     else
        local canCompound = ArtifactM.canCompound(data.classId) == true;
        local canUpgrade = ArtifactM.canUpgrade(data.classId) == true;

        local fileName = ItemM.query(data.classId + 200, "icon");
        iconImg:loadTexture(getItemIconPath(fileName));

        if canCompound then
            iconImg:setOpacity(255);
            playBubbleAnima(iconImg, false, 0.88);
            bg:setOpacity(125);
            TextStyleM.setTextStyle(stateLabel, TextStyleM.TEXT_SIZE_MINI, TextStyleM.TEXT_COLOR_BEIGE, true);
            stateLabel:setString(getLocStr("artifact_can_compound"));
            stateLabel:getVirtualRenderer():setAdditionalKerning(getLocKerning() -3);
            stateLabel:setVisible(false);
        else
            iconImg:setOpacity(125);
            bg:setOpacity(125);
        end
    end
end

-- 显示神器详细界面
function UIArtifactChoose:showDetailInfo(id)
    self.isLongPress = true;

    local uiForm = UIArtifactCompound.create(id, 1);
    UIMgr.getCurrentScene():addForm(uiForm);
end

-- 注册商品点击事件
function UIArtifactChoose:registerArtifactItemTouchEvent(node)
    -- 注册商品图标点击事件
    local bg = node:getChildByName("bg");
    local function onClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_click");

                if self.data[node.index] == nil then
                    return;
                end
                local lastSelected = self.selectIndex;

                self.selectIndex = node.index;

                self:artifacttSelected(lastSelected, node.index);
        end
    end
    bg:addTouchEventListener(onClick);
end

-- 注册事件处理回调函数
function UIArtifactChoose:registerEventCallback()
    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIArtifactChoose 界面析构清理");
            EventMgr.removeAll("UIArtifactChoose");

            local UIEnterDungeon = UIMgr.getCurrentScene():getFormByName("UIEnterDungeon");
            if UIEnterDungeon then
                UIEnterDungeon:setVisible(true);
            end
        elseif eventType == "enter" then
            local UIEnterDungeon = UIMgr.getCurrentScene():getFormByName("UIEnterDungeon");
            if UIEnterDungeon then
                UIEnterDungeon:setVisible(false);
            end
        end
    end);

    -- 关注修复成功的事件
    EventMgr.register("UIArtifactChoose", event.ARTIFACT_DURA_RECOVER, function(args)
        self:initData();
        self:redraw();
        self:jumpTo(args);
    end);

    -- 关注合成成功的事件
    EventMgr.register("UIArtifactChoose", event.ARTIFACT_COMPOUND, function(args)
       self:initData();
       self:redraw();
       self:jumpTo(args);
    end);
end

-- 注册点击事件
function UIArtifactChoose:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIArtifactChoose");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册维修按钮点击事件
    local btnRepair = findChildByName(self.node, "BT/repair_btn");
    btnRepair:setVisible(true);
    local function onRepairClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            local data = self.data[self.selectIndex];

            -- 打开神器维修界面
            if UIMgr:getCurrentScene():isOpen("UIArtifactRepair") then
                UIMgr:getCurrentScene():removeForm("UIArtifactRepair");
            end
            local uiArtifactRepair = UIArtifactRepair.create(data.classId);
            UIMgr.getCurrentScene():addForm(uiArtifactRepair);
        end
    end
    btnRepair:addTouchEventListener(onRepairClick);
    TextStyleM.setTextStyle(btnRepair, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnRepair:setTitleText(getLocStr("repair"));

    -- 注册选择按钮点击时间
    local btnSelect = findChildByName(self.node, "BT/choose_btn");
    btnSelect:setVisible(true);
    local function onSelectClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            local data = self.data[self.selectIndex];
            if not ArtifactM.canCarryArtifact(data.classId) then
                return;
            end

            EventMgr.fire(event.ARTIFACTT_CHOOSE_OK, data.classId);

            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIArtifactChoose");
        end
    end
    btnSelect:addTouchEventListener(onSelectClick);
    TextStyleM.setTextStyle(btnSelect, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnSelect:setTitleText(getLocStr("btn_text_select"));
end

-- 神器选中效果
function UIArtifactChoose:artifacttSelected(lastSelected, nowSelected)
    -- 清除上次选中效果
    local lastSel = self.scrollView:getChildByName("item" .. lastSelected);
    local focusBg = lastSel:getChildByName("focus");
    focusBg:setVisible(false);

    -- 新选中的效果
    local nowSel = self.scrollView:getChildByName("item" .. nowSelected);
    local nowFocusBg = nowSel:getChildByName("focus");
    nowFocusBg:setVisible(true);

    -- 更新说明和按钮
    local data = self.data[nowSelected];
    self:refreshExplain();
    -- 维修按钮

    -- 更新说明
    local data = self.data[self.selectIndex];
    local btnSelect = findChildByName(self.node, "BT/repair_btn");
    if data.isHas then
        self:redrawRepairBtn2();
        if data.isHas and data.durability < data.durabilityAll then
            btnSelect:setBright(true);
            btnSelect:setTouchEnabled(true);
        else
            btnSelect:setBright(false);
            btnSelect:setTouchEnabled(false);
         end
    else
        self:redrawRepairBtn();
        local canCompound = ArtifactM.canCompound(data.classId) == true;
        if canCompound then
            btnSelect:setBright(true);
            btnSelect:setTouchEnabled(true);
        else
            btnSelect:setBright(false);
            btnSelect:setTouchEnabled(false);
         end
    end

    -- 设置选择按钮
    local btnRepair = findChildByName(self.node, "BT/choose_btn");
    if data.isHas and data.durability > 0 then
        btnRepair:setBright(true);
        btnRepair:setTouchEnabled(true);
    else
        btnRepair:setBright(false);
        btnRepair:setTouchEnabled(false);
    end
end


-- 设置说明
function UIArtifactChoose:refreshExplain()
    -- 更新说明
    local data = self.data[self.selectIndex];
    -- 先把标题和描述隐藏起来
    local captionLabel = findChildByName(self.CT, "bg_2/caption_label");
    captionLabel:setVisible(false);
    local Label = findChildByName(self.CT, "bg_2/explain_scroll/explain");
    Label:setVisible(false);

    local durabilityLabel = findChildByName(self.CT, "bg_2/durability_label");
    durabilityLabel:setVisible(false);

    local scrapLabel = findChildByName(self.CT, "bg_2/scrap_label");
    scrapLabel:setVisible(false);

    local needScrapLabel = findChildByName(self.CT, "bg_2/need_scrap_label");
    needScrapLabel:setVisible(false);

--    -- 阵营
    local campLabel = findChildByName(self.CT, "bg_2/camp_label");
    campLabel:setVisible(false);

    -- 隐藏阵营
    local campIcon = findChildByName(self.CT, "bg_2/camp_icon");
    campIcon:setVisible(false);

    --
    local exLabel = findChildByName(self.CT, "bg_2/explain_scroll/explain1");
    exLabel:setVisible(false);

     -- 获取神器的名字
    local artifactName = data.name;
    local kerning = 0;

    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning = -2;
    end

    -- 如果神器已经激活则显示神器名称
    if data.isHas then
        captionLabel:setVisible(true);
        TextStyleM.setTextStyle(captionLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED, false, kerning);
        local level = ArtifactM.getArtifactLevel(data.classId);
        if level > 0 then
            -- 根据语言版本选择设置方式
            -- TODO 这是一个暂时解决问题的方法 SLIMEC-2042
            if getLang() == "zh" or getLang() == "tw" then
                applyString(captionLabel, artifactName .. " +" .. level);
            else
                captionLabel:setString(artifactName .. " +" .. level);
            end
        else
            if getLang() == "zh" or getLang() == "tw" then
                applyString(captionLabel, artifactName);
            else
                captionLabel:setString(artifactName);
            end
        end

        local explainText = "";
        local explainScroll = findChildByName(self.CT, "bg_2/explain_scroll");
        explainScroll:setInnerContainerSize(cc.size(explainScroll:getContentSize().width, 350));

        local explainLabel = findChildByName(self.CT, "bg_2/explain_scroll/explain1");
        explainLabel:setVisible(true);
        TextStyleM.setTextStyle(explainLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE);

        -- 神器的基础属性描述
        local props = EquipM.query(data.classId, "prop");
        local desc = EquipM.getEquipPropDesc(props);
        explainLabel:setString(desc);
        explainLabel:setPosition(self:getPositionX(), self.pos + 150);

        local labelheight = explainLabel:getContentSize().height;
        local labelY = explainLabel:getPositionY();

        local explainLabel2 = findChildByName(self.CT, "bg_2/explain_scroll/explain2");
        explainLabel2:setVisible(true);
        TextStyleM.setTextStyle(explainLabel2, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE);
        explainLabel2:setPosition(cc.p(explainLabel2:getPositionX(), labelY - labelheight - 10));  --


        -- 获取强化后神器的属性提升
        local desc2 = ArtifactM.getUpgradePropDesc(data.classId, level);
        applyString(explainLabel2, desc2);

        local str = string.format(getLocStr("durability_show"), data.durability, data.durabilityAll);

        durabilityLabel:setVisible(true);
        TextStyleM.setTextStyle(durabilityLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_DARK_BROWN);
        applyString(durabilityLabel, str);

        -- 碎片
        local scrapLabel = findChildByName(self.CT, "bg_2/scrap_label");

        local fragmentStr = string.format(getLocStr("fragment_show"), data.fragmentCount);
        scrapLabel:setVisible(true);
        TextStyleM.setTextStyle(scrapLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_DARK_BROWN);
        applyString(scrapLabel, fragmentStr);

    else
        -- 神器未激活
        captionLabel:setVisible(true);
        local str1;
        -- 根据语言版本选择设置方式
        -- TODO 这是一个暂时解决问题的方法 SLIMEC-2042
        if getLang() == "zh" or getLang() == "tw" then
            str1 = string.format(data.name..getLocStr("artifact_not_active"));
            TextStyleM.setTextStyle(captionLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED, false, kerning);
            applyString(captionLabel, str1);
        else
            str1 = data.name;
            TextStyleM.setTextStyle(captionLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
            captionLabel:setString(str1);
        end

        local explainScroll = findChildByName(self.CT, "bg_2/explain_scroll");
        explainScroll:setInnerContainerSize(cc.size(explainScroll:getContentSize().width, 200));
        local explain1 = findChildByName(self.CT, "bg_2/explain_scroll/explain1");
        explain1:setPosition(self:getPositionX(), self.pos);
        local desc = string.format(getLocStr("artifact_chip_not_gain"));
        explain1:setVisible(true);
        TextStyleM.setTextStyle(explain1, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
        explain1:setString(desc);

        local cost = FormulaM.invoke("CALC_ARTIFACT_UPGRADE_LEVEL_COST", 0, data.classId);
        local fragmentNeedCount = 10;
        for _, arr in pairs(cost) do
            if arr[1] == 1 then
                fragmentNeedCount = arr[3];
            end
        end

        local str = string.format(getLocStr("fragment_show1"), data.fragmentCount, fragmentNeedCount);
        local needScrapLabel = findChildByName(self.CT, "bg_2/need_scrap_label");
        TextStyleM.setTextStyle(needScrapLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE);
        needScrapLabel:setString(str);
        needScrapLabel:setVisible(true);

        local explainLabel2 = findChildByName(self.CT, "bg_2/explain_scroll/explain2");
        explainLabel2:setVisible(false);
    end
end

-- 重绘合成按钮
function UIArtifactChoose:redrawRepairBtn()
    local compoundBtn = findChildByName(self.node, "BT/repair_btn");
    compoundBtn:setTitleText(getLocStr("btn_text_compound"));
    local function onRepairClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            local level = AlchemyWorkshopM.getStrengthenLevel(16);

            if not(level > 0) then
                alert(getLocStr("no_artifact_workshop_building"));
                return;
            end

            local data = self.data[self.selectIndex];

               -- 打开神器维修界面
            if UIMgr:getCurrentScene():isOpen("UIArtifactCompound") then
                UIMgr:getCurrentScene():removeForm("UIArtifactCompound");
            end
            local uiArtifactCompound = UIArtifactCompound.create(data.classId, 1);
            UIMgr.getCurrentScene():addForm(uiArtifactCompound);
            AudioM.playFx("ui_open1");
        end
    end
    compoundBtn:addTouchEventListener(onRepairClick);
    TextStyleM.setTextStyle(compoundBtn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
end

-- 重绘维修按钮
function UIArtifactChoose:redrawRepairBtn2()

    -- 注册维修按钮点击事件
    local btnRepair = findChildByName(self.node, "BT/repair_btn");

    local function onRepairClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            local data = self.data[self.selectIndex];

            -- 打开神器维修界面
            if UIMgr:getCurrentScene():isOpen("UIArtifactRepair") then
                UIMgr:getCurrentScene():removeForm("UIArtifactRepair");
            end
            local uiArtifactRepair = UIArtifactRepair.create(data.classId);
            UIMgr.getCurrentScene():addForm(uiArtifactRepair);
            AudioM.playFx("ui_open1");
        end
    end
    btnRepair:addTouchEventListener(onRepairClick);
    TextStyleM.setTextStyle(btnRepair, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnRepair:setTitleText(getLocStr("repair"));
end

-- -- 播放升级光效
function UIArtifactChoose:playUpgradeEffect(id)
    local ids = {};
    for i = 1, #self.data do
        local info = self.data[i];
        table.insert(ids, info.classId);
    end

    local index = table.indexOf(ids, id);
    local grid = self.scrollView:getChildByName("item" .. index);

    local lightNode2 = findChildByName(grid, "bg");
    local function playWaiting()
        lightNode2:removeAllChildren();
        playEffect(lightNode2, 1180, 90, 12,  nil, 0.82);
    end
    for i = 1, 2 do
        local delay = i-1;
        performWithDelay(lightNode2, playWaiting, delay);
    end
end

-- 移动到页面中心
function UIArtifactChoose:jumpTo(args)
    local ids = {};
    for i = 1, #self.data do
         local info = self.data[i];
         table.insert(ids, info.classId);
    end

    local index = table.indexOf(ids, args["classId"]);

    local scrollview = findChildByName(self.node, "CT/scrollview");
    local percent = math.ceil(index/3) * 100 / math.ceil(#ids/3);
    if percent < 0 then
        percent = 0;
    end
    self.scrollView:jumpToPercentVertical(100 - percent);

    self.selectIndex = index;
    self:artifacttSelected(1, index);
end